package com.dexilaonian.adpter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.bean.AllorderBean;
import com.dexilaonian.bean.objBean;
import com.dexilaonian.fragment.AllOrderFragment;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;
public class AllOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private  Context context;
    private  List<AllorderBean> datas;
    private int normalType = 0;     // 第一种ViewType，正常的item
    private int footType = 1;       // 第二种ViewType，底部的提示View

    private boolean hasMore = true;   // 变量，是否有更多数据
    private boolean fadeTips = false; // 变量，是否隐藏了底部的提示
    private Handler mHandler = new Handler(Looper.getMainLooper()); //获取主线程的Handler

    public AllOrderAdapter(List<AllorderBean> datas, Context context, boolean hasMore) {
        // 初始化变量
        this.datas = datas;
        this.context = context;
        this.hasMore = hasMore;
        Log.e("TTTdatas", datas.size()+"="+hasMore);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 根据返回的ViewType，绑定不同的布局文件，这里只有两种
        if (viewType == normalType) {
            return new NormalHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_order, null));
        } else {
            return new FootHolder(LayoutInflater.from(context).inflate(R.layout.footview, null));
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }
    // 自定义方法，获取列表中数据源的最后一个位置，比getItemCount少1，因为不计上footView
    public int getRealLastPosition() {
        return datas.size();
    }
    // 正常item的ViewHolder，用以缓存findView操作
    class NormalHolder extends RecyclerView.ViewHolder {
     TextView idNum;
     TextView alspay;
     TextView titleOrder;
     TextView min_order;
     TextView allOrder_money;
     TextView order_time;
     ImageView ivAll;

        public NormalHolder(View itemView) {
            super(itemView);
            idNum = (TextView) itemView.findViewById(R.id.id_num);
            alspay = (TextView) itemView.findViewById(R.id.all_ispay);
            titleOrder = (TextView) itemView.findViewById(R.id.tv_title_order);
            min_order = (TextView) itemView.findViewById(R.id.tv_min_order);
            allOrder_money = (TextView) itemView.findViewById(R.id.tv_allOrder_money);
            order_time = (TextView) itemView.findViewById(R.id.tv_order_time);
            ivAll = (ImageView) itemView.findViewById(R.id.iv_all);
        }
    }

    // // 底部footView的ViewHolder，用以缓存findView操作
    class FootHolder extends RecyclerView.ViewHolder {
        private TextView tips;

        public FootHolder(View itemView) {
            super(itemView);
            tips = (TextView) itemView.findViewById(R.id.tips);
        }
    }
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof NormalHolder) {
            String Ref_no = datas.get(position).getRef_no();
            String status = datas.get(position).getStatus();
            int Other_id = datas.get(position).getOther_id();
            int type = datas.get(position).getType();
            objBean obj = datas.get(position).getObj();
            String actual_amount_paid = datas.get(position).getActual_amount_paid();
            if (obj != null) {
                String name = obj.getName();
                String pic_url = obj.getPic_url();
                String introduce = obj.getIntroduce();
                Object content_time = obj.getContent_time();
                if (!TextUtils.isEmpty(content_time+"")) {
                    ((NormalHolder) holder).order_time.setText(content_time + "分钟");
                }
                if (!TextUtils.isEmpty(pic_url)) {
                    String pro_url = Constant.base_pic_url + pic_url;
                    ImageLoader.display2(context, pro_url,  ((NormalHolder) holder).ivAll);
                }
                if (!TextUtils.isEmpty(name)) {
                    ((NormalHolder) holder).titleOrder.setText(name);
                }
                if (!TextUtils.isEmpty(introduce)) {
                    ((NormalHolder) holder).min_order.setText(introduce);
                }
            }
            if (!TextUtils.isEmpty(Ref_no)) {
                ((NormalHolder) holder).idNum.setText(Ref_no);
            }
            if (!TextUtils.isEmpty(status)) {
                ((NormalHolder) holder).alspay.setText(status);
            }


            if (!TextUtils.isEmpty(actual_amount_paid)) {
                ((NormalHolder) holder).allOrder_money.setText("￥"+actual_amount_paid);
            }

        } else {
            ((FootHolder) holder).tips.setVisibility(View.VISIBLE);
            if (hasMore == true) {
                fadeTips = false;
                if (datas.size() > 0) {
                    ((FootHolder) holder).tips.setText("正在加载更多...");
                }
            } else {
                if (datas.size() > 0) {
                    ((FootHolder) holder).tips.setText("没有更多数据了");
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ((FootHolder) holder).tips.setVisibility(View.GONE);
                            fadeTips = true;
                            hasMore = true;
                        }
                    }, 500);
                }
            }
        }
    }
    // 暴露接口，改变fadeTips的方法
    public boolean isFadeTips() {
        return fadeTips;
    }

    // 暴露接口，下拉刷新时，通过暴露方法将数据源置为空
    public void resetDatas() {
        datas = new ArrayList<>();
    }

    // 暴露接口，更新数据源，并修改hasMore的值，如果有增加数据，hasMore为true，否则为false
    public void updateList(List<AllorderBean> newDatas, boolean hasMore) {
        // 在原有的数据之上增加新数据
        if (newDatas != null) {
           datas.addAll(newDatas);
        }

        Log.e("TTTSIZE33", newDatas.size() + "="+datas);
        this.hasMore = hasMore;
        notifyDataSetChanged();
    }
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }
}